package lk.icta.core.dao.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.OccupierDAO;
import lk.icta.core.entity.Occupier;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Duminda Dharmakeerthi
 */
public class OccupierDAOImpl extends BaseDAO implements OccupierDAO {
    //TODO: remove this clas
    @Transactional(propagation = Propagation.MANDATORY)
    public void addOccupier(Occupier occupier, User user) {
        logger.debug("Adding an Occupier {} by {}", occupier.getName(), user.getUsername());
        em.persist(occupier);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateOccupier(Occupier occupier, User user) {
        logger.debug("Updating an Occupier {} by {}", occupier.getName(), user.getUsername());
        em.merge(occupier);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void removeOccupier(Occupier occupier, User user) {
        logger.debug("Removing occupier {} by {}", occupier.getId(), user.getUsername());
        em.remove(occupier);
//        em.flush();
    }

    public Occupier getOccupierById(long occupierId, User user) {
        logger.debug("Find occupier {} by {}", occupierId, user.getUsername());
        return em.find(Occupier.class, occupierId);
    }
}
